<?php

namespace app\common\model\marketing;

use addons\xyb\XypHelper;
use app\api\service\Activity\AssociationJobsService;
use think\Model;


class AssociationProgress extends Model
{

    // 表名
    protected $name = 'association_progress';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    protected $append = [
        'association_info',
        'jobs_info',
        'create_date',
        'examine_date'
    ];
    public function getAssociationInfoAttr($value, $data){
        if(isset($data['association_id']) && $data['association_id']){
            return \app\common\model\marketing\Association::where('id', $data['association_id'])->value('name');
        }
       return '';
    }
    public function getJobsInfoAttr($value, $data){
        if(isset($data['jobs_id']) && $data['jobs_id']){
            return \app\common\model\marketing\AssociationJobs::where('id', $data['jobs_id'])->find();
        }
        return '';
    }
    public function getTrendsDetailAttr($value, $data){
        if(!isset($data['trends_detail']) || !$data['trends_detail']){
            return [];
        }
        return XypHelper::formatJsonStringToArray($data['trends_detail']);
    }
    public function getCreateDateAttr($value, $data){
        if(isset($data['createtime']) && $data['createtime']) {
            return date('Y-m-d H:i', $data['createtime']);
        }else{
            return '';
        }
    }
    public function getExamineDateAttr($value, $data){
        if(isset($data['createtime']) && $data['createtime']) {
            return date('Y-m-d H:i', $data['createtime']);
        }else{
            return '';
        }
    }
    public static function getMissionAdminLists()
    {
        $association_id = request()->param('association_id/a');
        $keyword = request()->param('keyword', '');
        $talk_id = request()->param('talk_id/a', '');

        $reward_method = request()->param('reward_method/a');
        $push_method = request()->param('push_method/a');
        $grant_status = request()->param('grant_status/a');
        $field = 'id,association_id,jobs_id,circle_id,user_id,trends_id,trends_type,grant_status,status,createtime,trends_detail,examine_time';
        $query = self::field($field)->where('activity_method', 2);
        if ($association_id = XypHelper::stringToArray($association_id)) {
            $query->whereIn('association_id', $association_id);
        }
        if ($talk_id = XypHelper::stringToArray($talk_id)) {
            $query->whereIn('talk_id', $talk_id);
        }
        if ($keyword) {
            $query->where(function ($query) use ($keyword) {
                $query->where('id', 'like', "%{$keyword}%")
                    ->whereOr('name', 'like', "%{$keyword}%");
            });
        }
        if ($reward_method = XypHelper::stringToArray($reward_method)) {
            $query->whereIn('reward_method', $reward_method);
        }
        if ($push_method = XypHelper::stringToArray($push_method)) {
            $query->whereIn('push_method', $push_method);
        }
        if ($grant_status = XypHelper::stringToArray($grant_status)) {
            $query->whereIn('grant_status', $grant_status);
        }
        return $query->order('id desc')->paginate(10);
    }

    /*获取用户进度数量*/
    public static function getUsersAssociationProgressTotal($user_id, $association_id, $jobs_id, $association_key = '', $trends_type = 'image'){
        return AssociationProgress::where('user_id', $user_id)
            ->where('association_id', $association_id)
            ->where('jobs_id', $jobs_id)
            ->where('grant_status', 1)
            ->where('association_key', $association_key)
            ->where('trends_type', $trends_type)
            ->count();

    }

    /*创建用户任务进度表*/
    public static function createUsersAssociationProgress($insert){
        /*动态已完成*/
        if($progress = AssociationProgress::where('trends_id', $insert['trends_id'])->find()){
            $insert['updatetime'] = time();
            AssociationProgress::update($insert, ['id'=>$progress['id']]);
            return true;
        }
        $insert['createtime'] = time();
        $insert['updatetime'] = time();
        if($insert['jobs_id']){
            \app\common\model\AssociationJobs::where('id', $insert['jobs_id'])->setInc('involved_total');
        }
       $res = self::create($insert);
        return $res->id;
    }

    /*获取用户今日已参加的日常任务;已完成的*/
    public static function retrieveDailyTasksIdsUserParticipatedToday( $association_ids, $users_id ){
        return self::where('user_id', $users_id)
            ->where('activity_method', 2)
            ->where('grant_status', 1)
            ->whereIn('association_id', $association_ids)
            ->column('jobs_id');
    }

    /*验证用户是否报名*/
    public static function hasUsersJobsIsEnrollParticipate( $users_id, $jobs_id ){
        return self::where('user_id', $users_id)->where('jobs_id', $jobs_id)
            ->where('activity_method', 2)
          //  ->where('grant_status', 1)
            ->value('jobs_id');
    }

    public static function getExamineDetails($progress_id){
        $progress = self::where('id', $progress_id)->find();
        $progress->association = Association::where('id', $progress->association_id)->find();
        return $progress;
    }

    public static function examineProgress($progress_id, $status, $examine_method = 1, $examine_uid = 0){
        if(!$progress = self::where('id', $progress_id)->find()){
            return false;
        }
        if(!$jobs = AssociationJobs::where('id', $progress->jobs_id)->find()){
            return false;
        }
        $progress->examine_time = date('Y-m-d H:i:s');
        if($status == 1){
            $progress->grant_status = 1;
            $progress->status = $examine_method == 1?2:4;
        } else {
            $progress->grant_status = 2;
            $progress->status = $examine_method == 1?3:5;
        }
        if($jobs->reward_method < 3) {
            $progress->reward_stated = 1;
            $progress->receive_stated = 1;
            $progress->reward_time = time();
            $progress->receive_time = time();
        }
        $progress->save();
        if($jobs->reward_method > 2){
            /*发放优惠劵奖励*/
            AssociationJobsService::distributeJobsCouponsRewards($progress->user_id, $progress->jobs_id);
        }
        /*发放标签*/
        AssociationJobsService::distributeJobsLabels($progress->user_id, $progress->jobs_id);
        return true;
    }
}